package subject_set.newcoder;

/**
 * @author haomin
 * @date 2023/01/15 19:27
 **/

class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;
}

public class NC5 {
    public class Solution {
        /**
         *
         * @param root TreeNode类
         * @return int整型
         */
        int sum = 0;
        public int sumNumbers (TreeNode root) {
            // write code here
            if(root == null) return 0;
            return sumNumbersHelper(root, 0);
        }
        public int sumNumbersHelper(TreeNode root, int sum) {
            if(root == null) return 0;
            sum = sum * 10 + root.val;
            if(root.left == null && root.right == null) return sum;
            return sumNumbersHelper(root.left, sum) + sumNumbersHelper(root.right, sum);
        }
    }
}